package com.changli03.threadsafe.runnablesafe;

public class noSafeTest {
    public static void main(String[] args) {
        TicketThread tt1 = new TicketThread();
        //共用一个类
        Thread t1 = new Thread(tt1);
        Thread t2 = new Thread(tt1);
        Thread t3 = new Thread(tt1);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }
}
class TicketThread implements Runnable {
    int ticketnum = 100 ;
    Object lock = new Object();
    @Override
    public void run() {
        while (true){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
            synchronized (lock){
                if (ticketnum > 0){

                    System.out.println(Thread.currentThread().getName() + "票号为:" + ticketnum);
                    ticketnum--;
                }else {
                    break;
                }
            }

        }
    }
}
